package com.inxedu.os.common.alipay;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PaymentPersonal
{
    private static String paygateway = "https://mapi.alipay.com/gateway.do?";

    public static String createUrl(String service, String partner, String input_charset, String sign_type, String key, String out_trade_no, String subject, String payment_type, String logistics_fee, String logistics_payment, String seller_email, String price, String quantity, String notify_url, String return_url, String logistics_type) { Map params = new HashMap();
        params.put("_input_charset", input_charset);
        params.put("logistics_fee", logistics_fee);
        params.put("logistics_payment", logistics_payment);
        params.put("notify_url", notify_url);
        params.put("logistics_type", logistics_type);
        params.put("out_trade_no", out_trade_no);
        params.put("partner", partner);
        params.put("payment_type", payment_type);
        params.put("price", price);
        params.put("quantity", quantity);
        params.put("return_url", return_url);
        params.put("seller_email", seller_email);
        params.put("service", service);
        params.put("subject", subject);

        String sign = Md5Encrypt.md5(getContent(params, key));
        String sendMessage = "";
        sendMessage = sendMessage + paygateway;
        List keys = new ArrayList(params.keySet());
        for (int i = 0; i < keys.size(); i++) {
            try
            {
                sendMessage = sendMessage + (String)keys.get(i) + "=" + URLEncoder.encode((String)params.get(keys.get(i)), input_charset) + "&";
            }
            catch (UnsupportedEncodingException e)
            {
                e.printStackTrace();
            }
        }
        sendMessage = sendMessage + "sign=" + sign + "&sign_type=" + sign_type;
        return sendMessage;
    }

    private static String getContent(Map<String, String> params, String privateKey)
    {
        List keys = new ArrayList(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = (String)keys.get(i);
            String value = (String)params.get(key);
            if (i == keys.size() - 1)
                prestr = String.valueOf(prestr) + key + "=" + value;
            else {
                prestr = String.valueOf(prestr) + key + "=" + value + "&";
            }
        }
        return String.valueOf(prestr) + privateKey;
    }
}